package com.baibei.ebec.user.reset;

import android.content.Context;
import android.text.TextUtils;

import com.baibei.basic.BasicPresenterImpl;
import com.baibei.basic.module.utils.SMSCountDownTimer;
import com.baibei.basic.module.validator.PhoneValidator;
import com.baibei.ebec.sdk.ApiFactory;
import com.baibei.ebec.sdk.IPushApi;
import com.baibei.ebec.sdk.IUserApi;
import com.baibei.sdk.ApiDefaultObserver;
import com.baibei.sdk.Empty;


/**
 * 重置密码
 * Created by ChenRui on 2017/5/23 0023 14:26.
 */
public class ResetPasswordPresenterImpl extends BasicPresenterImpl<ResetPasswordContract.View> implements ResetPasswordContract.Presenter {

    private IPushApi mPushApi;
    private IUserApi mUserApi;
    private final SMSCountDownTimer mCountDownTimer = new SMSCountDownTimer();
    private PhoneValidator mPhoneValidator = new PhoneValidator();


    public ResetPasswordPresenterImpl(Context context, ResetPasswordContract.View view) {
        super(context, view);
        mPushApi = ApiFactory.getInstance().getPushApi();
        mUserApi = ApiFactory.getInstance().getUserApi();
    }

    @Override
    public void sendSms() {
        if (!isMobileNum(mView.getPhoneNumber())) {
            mView.onSmsFailed("请输入正确的手机号码");
            return;
        }

        // 发送成功，开始倒计时
        startCountDown();
        createObservable(mPushApi.sendSMS(IPushApi.SMS_TYPE_LOGIN_PASSWORD, mView.getPhoneNumber(), null))
                .subscribe(new ApiDefaultObserver<Empty>() {
                    @Override
                    protected void onError(String message) {
                        mView.onSmsFailed(message);
                        stopCountDown();
                    }

                    @Override
                    protected void accept(Empty emptyResponse) {
                        mView.onSmsSuccess();
                    }
                })
        ;
    }

    public boolean isMobileNum(String mobiles) {
        String telRegex = "[1]\\d{10}";
        return TextUtils.isEmpty(mobiles)?false:mobiles.matches(telRegex);
    }

    @Override
    public void resetPassword() {

        String mobile = mView.getPhoneNumber().trim();
        String password = mView.getPassword().trim();
        String smsCode = mView.getSmsCode().trim();

        if (password.length() < 6 || password.length() > 15) {
            mView.onResetPasswordFailed("密码长度为6-15位");
            return;
        }
        if (!isMobileNum(mView.getPhoneNumber())) {
            mView.onSmsFailed("请输入正确的手机号码");
            return;
        }

        createObservable(mUserApi.resetLoginPassword(mobile, password, smsCode))
                .subscribe(new ApiDefaultObserver<Empty>() {
                    @Override
                    protected void onError(String message) {
                        mView.onResetPasswordFailed(message);
                    }

                    @Override
                    protected void accept(Empty emptyResponse) {
                        mView.onResetPasswordSuccess();
                    }
                });
    }

    @Override
    public boolean isCountDownFinish() {
        return mCountDownTimer.isFinish();
    }

    /**
     * 开始倒计时
     */
    private void startCountDown() {
        mCountDownTimer.start(mView);
    }

    /**
     * 停止倒计时
     */
    private void stopCountDown() {
        mCountDownTimer.finish();
    }

    @Override
    public void destroy() {
        super.destroy();
        mCountDownTimer.finish();
        mPushApi = null;
        mUserApi = null;
    }
}
